package com.ly.test.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.net.InetAddress;

/**
* @author liuyi
* @date 2017/01/17 9:46
*/
//@Aspect
//@Component
public class MyAutoAspect {
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(MyAutoAspect.class);

    @Pointcut("execution(* com.ly.test.controller..*.*(..))")
    public void webRequest(){}

//    @Before("webLog()")
//    public void doBefore(JoinPoint joinPoint) throws Throwable {
//        // 接收到请求，记录请求内容
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//
//        // 记录下请求内容
//        logger.info("URL : " + request.getRequestURL().toString());
//        logger.info("HTTP_METHOD : " + request.getMethod());
//        logger.info("IP : " + request.getRemoteAddr());
//        logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
//        logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
//
//    }
//
//    @AfterReturning(returning = "ret", pointcut = "webLog()")
//    public void doAfterReturning(Object ret) throws Throwable {
//        // 处理完请求，返回内容
//        logger.info("RESPONSE : " + ret);
//    }

//    @Around("webRequest()")
    public Object handleAop(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature methodSignature = (MethodSignature)pjp.getSignature();
        Method method = methodSignature.getMethod();
//        MDC.put("ip", InetAddress.getLocalHost().getHostAddress());
        logger.info("-------------------" + method);
//        logger.info();
        Object o = pjp.proceed();
//        logger.info("+++++++++++++++++++");
//        MDC.remove("ip");
        return o;
    }
}
